package es.uji.viselab.benchmark;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

import es.uji.viselab.BenchMarkingSystem;
import es.uji.viselab.LogLevel;
import es.uji.viselab.LogRecorder;
import es.uji.viselab.benchmark.database.BMDatabaseManager;
import es.uji.viselab.benchmark.database.DBConnection;
import es.uji.viselab.benchmark.database.DBConnectionMySQL;
import es.uji.viselab.benchmark.database.DBNonPersistent;
import es.uji.viselab.benchmark.process.BenchMarkingProcessor;
import es.uji.viselab.benchmark.process.GNUPlot;
import es.uji.viselab.benchmark.process.JPlot;
import es.uji.viselab.benchmark.source.Source;
import es.uji.viselab.benchmark.source.VispSource;
import es.uji.viselab.benchmark.test.BMSimulationThread;
import es.uji.viselab.experiment.Experiment;
import es.uji.viselab.experiment.MoveExperiment;
import es.uji.viselab.experiment.WaitExperiment;
import es.uji.viselab.network.Server;

public class BenchMarkingInterface implements ActionListener {

	// Source source;
	// Experiment experiment;
	// Server server;

	BenchMarkingRecorder bm;

	protected JFrame mainWindow;

	// protected Button buttonStartServer = new Button("Start");
	protected Button buttonPlotData = new Button("Process data");
	// protected Button buttonStopServer = new Button("Stop server");
	protected Button buttonTest = new Button("Simulation");
	//protected Label labelVerbosity = new Label("Verbosity (log): ");
	//protected JComboBox availableVerbosityLevel;
	// protected Label labelExperiments = new Label("Experiment: ");
	// protected JComboBox availableExperiments;
	protected Label labelDatabase = new Label("Database: ");
	protected JComboBox availableDatabases;
	protected Label labelProcess = new Label("Process: ");
	protected JComboBox availableProcess;
	// protected Label labelSource = new Label("Source link: ");
	// protected JComboBox availableSources;
	//protected TextArea textLog = new TextArea(10, 60);

	/*
	 * public BenchMarkingInterface(JFrame mainWindow) { this.mainWindow =
	 * mainWindow; }
	 */

	public BenchMarkingInterface(BenchMarkingRecorder bm) {
		this.bm = bm;
		this.mainWindow = new JFrame();
	}

	public JPanel getMainMenu() {
		JPanel mainMenu = new JPanel();
		mainMenu.setLayout(new BoxLayout(mainMenu, BoxLayout.PAGE_AXIS));

		String verboseLevels[] = { "Error", "Warning", "Info", "Detail", "Debug", "Stats" };
		//availableVerbosityLevel = new JComboBox(verboseLevels);
		// Default value
		//availableVerbosityLevel.setSelectedIndex(5);
		//LogRecorder.setLogLevel(LogLevel.STATS);
		//LogRecorder.setTextAreaLog(textLog);
		//LogRecorder.log(LogLevel.INFO, "Current level of log set to STATS");

		// String availableSourceList[] = { "ViSP (sockets)" };
		// availableSources = new JComboBox(availableSourceList);

		String availableDatabasesList[] = { "MySQL on localhost", "Memory DB" };
		availableDatabases = new JComboBox(availableDatabasesList);

		String availableProcessList[] = { "Java Plot", "GNUPlot" };
		availableProcess = new JComboBox(availableProcessList);
		availableProcess.setSelectedIndex(1);

		// String availableExperimentsList[] = { "Wait and receive",
		// "Ask and send" };
		// availableExperiments = new JComboBox(availableExperimentsList);
		// availableExperiments.setSelectedIndex(0);

		JPanel line1 = new JPanel();
		line1.setLayout(new FlowLayout());
		((FlowLayout) line1.getLayout()).setAlignment(FlowLayout.CENTER);

		line1.add(labelDatabase);
		line1.add(availableDatabases);

		JPanel line2 = new JPanel();
		line2.setLayout(new FlowLayout());

		// line2.add(buttonTest);

		JPanel line3 = new JPanel();
		line3.setLayout(new FlowLayout());

		line3.add(labelProcess);
		line3.add(availableProcess);

		line3.add(buttonPlotData);

		JPanel line4 = new JPanel();
		line4.setLayout(new FlowLayout());

		//line4.add(labelVerbosity);
		//line4.add(availableVerbosityLevel);

		JPanel line5 = new JPanel();
		line5.setLayout(new FlowLayout());

		//line5.add(textLog);

		mainMenu.add(line1);
		mainMenu.add(line2);
		mainMenu.add(line3);
		mainMenu.add(line4);
		mainMenu.add(line5);

		buttonPlotData.addActionListener(this);
		buttonTest.addActionListener(this);
		return mainMenu;
	}

	@Override
	public void actionPerformed(ActionEvent ae) {

		BenchMarkingProcessor processor = null;
		if (ae.getSource().equals(buttonPlotData)) {

			switch (availableProcess.getSelectedIndex()) {
			case 0:
				processor = new JPlot();
				bm.setProcessor(processor);
				break;
			case 1:
				processor = new GNUPlot();
				bm.setProcessor(processor);
				break;
			}

			processor.setDataSource(bm.getDatabase());
			processor.run();
		}
		if (ae.getSource().equals(buttonTest)) {
			BMSimulationThread simulation = new BMSimulationThread();
			Thread simulationThread = new Thread(simulation);
			simulationThread.start();
		}
	}

	public void start() {

		// Where the data will be stored ?
		BMDatabaseManager db = BMDatabaseManager.getInstance();
		switch (availableDatabases.getSelectedIndex()) {
		case 0:
			db.setConnection(new DBConnectionMySQL());
			break;
		case 1:
			db.setConnection(new DBNonPersistent());
			break;
		default:
			LogRecorder.log(LogLevel.ERROR, "Option for database not recognised.");
		}

		DBConnection database = db.getConnection();
		if (database != null) {
			database.Connect();
		}

		bm.setDatabase(database);
		bm.start();

	}

	public JPanel getPanel() {
		return getMainMenu();
	}

}
